<template>
  <div>
    <TopFilter ref="topFilter" :list="topFilterList" @filterChange="handleSearch"></TopFilter>
    <FilterTable
      ref="table"
      :columns="columns"
      height="auto"
      :maxHeight="350"
      selectionType="single"
      :fetchapi="fetchapi"
      :params="fetchParams"
      :datakey="'rows'"
      :onSyncTableData="handleSyncTableData"
      :onRowSelectChange="handleTableRowSelect"
      :onColumnsChange="columns => (this.columns = columns)"
    >
    </FilterTable>

    <div class="dialogBottmView">
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="handleConfirm">确定</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { confirmAction, notifyAction, findFormItem, findTableItem } from '@/utils';

import { queryRepairEstimateOrder } from '@/api/repair/repairReception/evaluation';
import { getDealerUser } from '@/api/repair/common';
import { getWxlxSelect } from '@/api/repair/repairReception/customer';

export default {
  mixins: [dictionary],
  data() {
    this.selectedTableRow = []; // 选中的table行项
    return {
      topFilterList: this.createTopFIlterList(),
      columns: this.createTableColumns(),
      fetchapi: queryRepairEstimateOrder,
      fetchParams: { xhrAbort: false },
      syncTableData: [] // 同步的table数据
    };
  },
  mounted() {
    this.initDicts();
  },
  methods: {
    createTopFIlterList() {
      return [
        {
          type: 'INPUT',
          label: '估价单号',
          fieldName: 'estimateNo'
        },
        {
          type: 'INPUT',
          label: '车牌号',
          fieldName: 'license'
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '估价单号',
          dataIndex: 'ESTIMATE_NO',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '维修类型',
          dataIndex: 'REPAIR_TYPE_CODE',
          dictItems: [],
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车牌号',
          dataIndex: 'LICENSE',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车主',
          dataIndex: 'OWNER_NAME',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '服务顾问',
          dataIndex: 'SERVICE_ADVISOR',
          sorter: true,
          filter: true,
          dictItems: [],
          filterType: 'input'
        },
        {
          title: '开单日期',
          dataIndex: 'CREATED_AT',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '操作员',
          dataIndex: 'CREATED_BY',
          dictItems: [],
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    // 初始化字典项
    initDicts() {
      this.queryRepairTypeDict();
      this.queryDealerUser();
    },
    // 获取维修类型
    async queryRepairTypeDict() {
      let res = await getWxlxSelect();
      if (res.resultCode == 200) {
        this.repairTypeList = res.data;
        let repairTypeDict = res.data.map(item => ({ text: item.REPAIR_TYPE_NAME, value: item.REPAIR_TYPE_CODE + '' }));
        let laboutTableRepairType = findTableItem(this.columns, 'REPAIR_TYPE_CODE');
        laboutTableRepairType.dictItems = laboutTableRepairType.editItems = laboutTableRepairType.filterItems = repairTypeDict;
      }
    },
    // 查询所有人员(翻译用)
    async queryDealerUser() {
      let res = await getDealerUser();
      if (res.resultCode == 200) {
        let userDicts = res.data.map(item => ({ text: item.employeeName, value: item.userId + '' }));
        let tableTargets = [findTableItem(this.columns, 'SERVICE_ADVISOR'), findTableItem(this.columns, 'CREATED_BY')];
        tableTargets.forEach(item => {
          item.dictItems = item.filterItmes = userDicts;
        });
      }
    },
    // 点击搜索
    handleSearch(val) {
      this.fetchParams = { ...this.fetchParams, ...val };
    },
    // 同步table数据
    handleSyncTableData(list) {
      this.syncTableData = list;
    },
    // table行项选择
    handleTableRowSelect(rows) {
      this.selectedTableRow = rows;
    },
    // 点击确定
    handleConfirm() {
      if (!this.selectedTableRow.length) return notifyAction('请至少选择一项', 'warning');
      this.$emit('onConfirm', this.selectedTableRow);
      this.handleClose();
    },
    // 点击取消
    handleClose() {
      this.$emit('close');
    }
  }
};
</script>

<style scoped></style>
